
package com.william.killian.trivia;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.william.killian.trivia.model.Quiz;
import com.william.killian.trivia.util.Util;

public class SplashScreen extends Activity
{

    private class EventHandler extends Handler
    {

        public void handleMessage (Message msg)
        {
            SplashScreen.this.startActivity (new Intent (SplashScreen.this, MainMenu.class));
            SplashScreen.this.finish ();
        }
    };

    @Override
    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate (savedInstanceState);
        setTheme (android.R.style.Theme_Light);
        setContentView (R.layout.splash);

        try
        {
            PackageManager pm = getPackageManager ();
            PackageInfo pi = pm.getPackageInfo ("com.william.killian.trivia", PackageManager.GET_META_DATA);
            TextView version = (TextView)findViewById (R.id.splash_version);
            version.setText ("v" + pi.versionName);
        }
        catch (NameNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        ImageView image = (ImageView) findViewById(R.id.splash_image);
        Animation anim = AnimationUtils.loadAnimation (this, R.anim.zoom);
        anim.setRepeatCount (Animation.INFINITE);
        anim.setRepeatMode (Animation.REVERSE);
        image.startAnimation(anim);

        final EventHandler timer = new EventHandler ();
        Util.SetAppPath (getFilesDir ());
        new Thread ()
        {
            public void run ()
            {
                Util.CopyFromAssetsToSDCard (SplashScreen.this);
                SplashScreen.this.loadQuizzes ();
                timer.sendEmptyMessageDelayed (0, 1000);
            }
        }.start ();
    }

    public void close (View v)
    {
        finish ();
    }
    

    private void loadQuizzes ()
    {
        Util.quizzes = new ArrayList <Quiz> ();
        File dir = Util.getApplicationDirectory ();
        for (String quizFile : dir.list (new XMLFilter ()))
        {
            File file = new File (dir, quizFile);
            Quiz q = Quiz.Load (file);
            if (q != null)
            {
                Util.quizzes.add (q);
            }
        }
    }

    
    private static final class XMLFilter implements FilenameFilter
    {
        public boolean accept (File dir, String filename)
        {
            return filename.endsWith ("xml");
        }
    }
}
